package org.example.controller.supplier;

import org.example.entity.Supplier;
import org.example.service.SupplierService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/supplier/updateSupplier")
public class updateSupplierController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收超链接传递过来的id值
        String id = req.getParameter("id");
        //确定删除哪个记录
        Supplier supplier = new Supplier();
        supplier.setId(Integer.parseInt(id));  //接收的字符串转换为整型id
        //调用supplier的实现类和方法
        SupplierService supplierService = new SupplierService();
        //查询到该记录的所有信息，传递给前端页面
        try {
            Supplier supplier1 = supplierService.queryOneSupplier(supplier);
            req.setAttribute("data",supplier1);
            req.getRequestDispatcher("/suppliers/updateSupplier.jsp").forward(req,resp);
            } catch (SQLException ex) {
            throw new RuntimeException(ex);
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
